/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.excesys.exsecs.om.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.common.log.annotation.SysLog;
import com.excesys.exsecs.om.dto.*;
import com.excesys.exsecs.om.entity.OmDeviceModelClassification;
import com.excesys.exsecs.om.entity.OmEvenAndDevice;
import com.excesys.exsecs.om.entity.OmInspectionItems;
import com.excesys.exsecs.om.mapper.OmEvenAndDeviceMapper;
import com.excesys.exsecs.om.service.OmDeviceModelClassificationService;
import com.excesys.exsecs.om.service.OmEventAndDeviceService;
import com.excesys.exsecs.om.service.OmInspectionItemsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 巡查区域与设备
 *
 * @author
 * @date 2023-05-19
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/ominspectionEquipment")
@Tag(name = "巡查区域设备与事项")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class OmInspectionEquipmentController {

	private final OmInspectionItemsService omInspectionItemsService;

	private final OmEventAndDeviceService omEventAndDeviceService;

	private final OmDeviceModelClassificationService omDeviceModelClassificationService;

	private final OmEvenAndDeviceMapper omEvenAndDeviceMapper;

	/**
	 * 新增巡检设备事项
	 *
	 * @param deviceAndModelDto 巡检区域设备dto
	 * @return R
	 */
	@Operation(summary = "新增巡检设备列表", description = "新增巡检设备事项")
	@SysLog("新增巡检设备事项")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('om_omequipmentList_add')")
	public R save(@RequestBody DeviceAndModelDto deviceAndModelDto) {
		omInspectionItemsService.addDevice(deviceAndModelDto);
		return R.ok();
	}

	/**
	 * 修改巡检区域事项显隐
	 *
	 * @param omInspectionItems 巡检区域事项列表
	 * @return R
	 */
	@Operation(summary = "修改巡检区域事项显隐", description = "修改巡检区域事项显隐")
	@SysLog("修改巡检区域事项显隐")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('om_omequipmentList_edit')")
	public R updateById(@RequestBody OmInspectionItems omInspectionItems) {
		return R.ok(omInspectionItemsService.updateById(omInspectionItems));
	}

	/**
	 * 通过id删除展示巡检事项
	 *
	 * @return R
	 */
	@Operation(summary = "通过id删除巡检事项", description = "通过id删除巡检事项")
	@SysLog("通过id删除巡检事项")
	@PostMapping("/deleteOutById")
	@PreAuthorize("@pms.hasPermission('om_omequipmentList_del')")
	public R removeOutById(@RequestBody EvenDeleteDto dto) {
		if (dto.getRegionFacilityCode() != null && dto.getRegionFacilityCode() != "") {
			omDeviceModelClassificationService.deleteByCode(dto.getRegionFacilityCode(), dto.getId());
			List<OmDeviceModelClassification> list = omDeviceModelClassificationService.getchlList(dto.getRegionCode());
			if (list.size() <= 0) {
				omDeviceModelClassificationService.deleteAll(dto.getRegionCode());
				return R.ok();
			}
		}
		return R.ok();
	}

	/**
	 * 通过id删除新增巡检事项
	 *
	 * @return R
	 */
	@Operation(summary = "通过id删除巡检事项", description = "通过id删除巡检事项")
	@SysLog("通过id删除巡检事项")
	@PostMapping("/deleteInById")
	@PreAuthorize("@pms.hasPermission('om_omequipmentList_del')")
	public R removeInById(@RequestBody EvenDeleteInDto dto) {
		omEventAndDeviceService.deleteByCode(dto.getRegionFacilityCode(), dto.getId());
		return R.ok(omInspectionItemsService.removeById(dto.getId()));
	}

	/**
	 * 通过id删除新增巡检事项
	 *
	 * @return R
	 */
	@Operation(summary = "通过id删除巡检事项", description = "通过id删除巡检事项")
	@SysLog("通过id删除巡检事项")
	@PostMapping("/deleteInByChildId")
	@PreAuthorize("@pms.hasPermission('om_omequipmentList_del')")
	public R removeInByChlId(@RequestBody EvenDeleteInDto dto) {
		omDeviceModelClassificationService.deleteBychlCode(dto.getRegionFacilityCode(), dto.getId());
		omInspectionItemsService.removeById(dto.getId());
		return R.ok();
	}

	/**
	 * 通过id查询巡检区域列表
	 *
	 * @param exId id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{exId}")
	@PreAuthorize("@pms.hasPermission('om_omequipmentList_get')")
	public R getById(@PathVariable("exId") Long exId) {
		List<OmEvenAndDevice> list = omEvenAndDeviceMapper.getList(exId);
		List<OmInspectionItems> omInspectionItemsList = new ArrayList<>();
		for (OmEvenAndDevice omEvenAndDevice : list) {
			omInspectionItemsList.add(omInspectionItemsService.getById(omEvenAndDevice.getItemsId()));
		}
		return R.ok(omInspectionItemsList);
	}

	/**
	 * 分页查询
	 *
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('om_omequipmentList_get')")
	public R getOmICheckPlanPage(Page page, ItemReqDto itemReqDto) {
		List<EventAndRegionRespDto> eventAndRegionRespDto = omInspectionItemsService
				.getDeviceList(itemReqDto.getRegionFacilityName(), itemReqDto.getDepartmentName());
		Page pages = omInspectionItemsService.setPage(eventAndRegionRespDto, page);
		return R.ok(pages);
	}

	/**
	 * 根据分类查询
	 *
	 * @return
	 */
	@Operation(summary = "根据分类查询", description = "根据分类查询")
	@GetMapping("/queryItems/{id}")
	@PreAuthorize("@pms.hasPermission('om_omequipmentList_get')")
	public R getItems(@PathVariable("id") String id) {
		List<OmInspectionItems> list = omInspectionItemsService.getQuipmentItems(id);
		return R.ok(list);
	}

	/**
	 * 根据分类明细查询
	 *
	 * @return
	 */
	@Operation(summary = "根据分类查询", description = "根据分类查询")
	@PostMapping("/queryItemsList")
	@PreAuthorize("@pms.hasPermission('om_inspection_area_items_get')")
	public R getItemsList(@RequestBody IdDto idDto) {
		List<OmInspectionItems> list = omInspectionItemsService.getQuipmentItemsList(idDto.getPidList());
		return R.ok(list);
	}

	/**
	 * 同步子类巡检项到父类
	 */
	@Operation(summary = "同步子类巡检项到父类", description = "同步子类巡检项到父类")
	@PostMapping("/synchronizationItems")
	@PreAuthorize("@pms.hasPermission('om_omequipmentList_add')")
	public R synchronizationItems(@RequestBody SynchronizationDto synchronizationDto) {
		List<OmInspectionItems> omInspectionItems = omInspectionItemsService
				.getItemsByInfo(synchronizationDto.getEvenInfo(), synchronizationDto.getEvenCriterion());
		for (OmInspectionItems items : omInspectionItems) {
			if (synchronizationDto.getId().equals(items.getId())) {
				return R.ok();
			}
		}
		OmInspectionItems inspectionItems = new OmInspectionItems();
		inspectionItems.setEvenInfo(synchronizationDto.getEvenInfo());
		inspectionItems.setEvenSwitch(synchronizationDto.getEvenSwitch());
		inspectionItems.setEvenCriterion(synchronizationDto.getEvenCriterion());
		omInspectionItemsService.save(inspectionItems);
		OmEvenAndDevice oe = new OmEvenAndDevice();
		oe.setDeviceCode(synchronizationDto.getPid());
		oe.setDeviceName(synchronizationDto.getPname());
		oe.setItemsId(synchronizationDto.getId());
		omEventAndDeviceService.save(oe);
		OmDeviceModelClassification omDeviceModelClassification = omDeviceModelClassificationService
				.getListByItemId(synchronizationDto.getId());
		List<OmDeviceModelClassification> classifications = omDeviceModelClassificationService.getchlByItemsList(
				synchronizationDto.getId(), omDeviceModelClassification.getDeviceModelClassification());
		for (OmDeviceModelClassification omDeviceModelClassification1 : classifications) {
			OmInspectionItems omInspectionItems2 = new OmInspectionItems();
			omInspectionItems2.setEvenInfo(synchronizationDto.getEvenInfo());
			omInspectionItems2.setEvenCriterion(synchronizationDto.getEvenCriterion());
			omInspectionItems2.setEvenSwitch(synchronizationDto.getEvenSwitch());
			omInspectionItemsService.save(omInspectionItems2);
			OmDeviceModelClassification facility = new OmDeviceModelClassification();
			facility.setDeviceModelClassification(omDeviceModelClassification1.getDeviceModelClassification());
			facility.setDeviceModelClassificationName(omDeviceModelClassification1.getDeviceModelClassificationName());
			facility.setItemsId(omInspectionItems2.getId().toString());
			facility.setParentId(synchronizationDto.getPid());
			omDeviceModelClassificationService.save(facility);
		}
		return R.ok();
	}

	/**
	 * 批量删除巡检项
	 */
	@Operation(summary = "批量删除巡检项", description = "批量删除巡检项")
	@PostMapping("/batchDelDevice")
	@PreAuthorize("@pms.hasPermission('om_inspection_area_items_del')")
	public R batchDelDevice(@RequestBody List<DeleteDto> deleteDtoList) {
		return R.ok(omInspectionItemsService.deleteDevice(deleteDtoList));
	}

}
